package com.topideal.supplychain.hades.service.impl;

import com.daxia.framework.common.util.DateUtils;
import com.google.common.collect.Lists;
import com.topideal.supplychain.framework.exception.BusinessException;
import com.topideal.supplychain.hades.dict.IssueStatusEnum;
import com.topideal.supplychain.hades.dict.IssueTypeEnum;
import com.topideal.supplychain.hades.dto.PersonalViewDto;
import com.topideal.supplychain.hades.mapper.PersonalListMapper;
import com.topideal.supplychain.hades.service.PersonalTimeCountService;
import com.topideal.supplychain.hades.service.ProjectTimeCountService;
import com.topideal.supplychain.hades.util.ExcelData;
import com.topideal.supplychain.hades.util.ExportExcelUtils;
import com.topideal.supplychain.hades.viewfilter.BasicInfoFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 个人工时统计
 *
 * @author xuxiaoyan
 * @date 2018-09-13 18:02
 */
@Service
public class PersonalTimeCountServiceImpl implements PersonalTimeCountService {

    @Autowired
    private PersonalListMapper personalListMapper;
    @Autowired
    private ProjectTimeCountService projectTimeCountService;

    /**
     * 个人工时统计 按人员维度+jira创建时间 汇总所有登记的工时
     *
     * @param basicInfoFilter
     * @return
     */
    @Override
    public List<PersonalViewDto> selectTimeSummary(BasicInfoFilter basicInfoFilter) {
        List<PersonalViewDto> personalViewDtos = personalListMapper.selectTimeSummary(basicInfoFilter);
        if (!CollectionUtils.isEmpty(personalViewDtos)) {
            // userKey转中文名
            engNameToChinaName(personalViewDtos);
        }
        return personalViewDtos;
    }

    /**
     * 个人工时jira任务详情统计 默认合并逻辑
     *
     * @param filter
     * @return
     */
    @Override
    public List<PersonalViewDto> selectTimeDetail(BasicInfoFilter filter) {
        List<PersonalViewDto> personalViewDtos = personalListMapper.selectTimeDetail(filter);
        if (!CollectionUtils.isEmpty(personalViewDtos)) {
            // 详情填充
            fill(personalViewDtos);
            // userKey转中文名
            engNameToChinaName(personalViewDtos);
        }
        return personalViewDtos;
    }

    /**
     * 个人工时jira任务详情统计 不合并逻辑，取每次登记的工时记录
     *
     * @param filter
     * @return
     */
    @Override
    public List<PersonalViewDto> selectEveryTimeDetail(BasicInfoFilter filter) {
        List<PersonalViewDto> personalViewDtos = personalListMapper.selectEveryTimeDetail(filter);
        if (!CollectionUtils.isEmpty(personalViewDtos)) {
            // 详情填充
            fill(personalViewDtos);
            // userKey转中文名
            engNameToChinaName(personalViewDtos);
        }
        return personalViewDtos;
    }


    /**
     * userKey转中文名
     *
     * @param personalViewDtos
     */
    private void engNameToChinaName(List<PersonalViewDto> personalViewDtos) {
        for (PersonalViewDto personalViewDto : personalViewDtos) {
            String userName = projectTimeCountService.selectChinaName(personalViewDto.getUserKey());
            personalViewDto.setUserName(userName);
        }
    }

    /**
     * 详情填充:
     * 1.将jira任务状态跟类型转化成中文
     * 2.时间格式统一
     *
     * @param personalViewDtos
     */
    private void fill(List<PersonalViewDto> personalViewDtos) {
        for (PersonalViewDto personalViewDto : personalViewDtos) {
            String typeName = IssueTypeEnum.getNameByValue(personalViewDto.getIssueType());
            String statusName = IssueStatusEnum.getNameByValue(personalViewDto.getIssueStatus());
            Date issueCreateTime = personalViewDto.getIssueCreateTime();
            personalViewDto.setIssueCreateTimeStr(DateUtils.dateToString(issueCreateTime, DateUtils.DATETIME_PATTERN));
            personalViewDto.setIssueType(typeName);
            personalViewDto.setIssueStatus(statusName);
        }
    }

    /**
     * 人员工时统计列表导出
     *
     * @param basicInfoFilter
     * @param response
     * @throws IOException
     */
    @Override
    public void exportPersonalListExcel(BasicInfoFilter basicInfoFilter, HttpServletResponse response) throws IOException {
        List<PersonalViewDto> personalViewDtos = selectTimeSummary(basicInfoFilter);

        if (CollectionUtils.isEmpty(personalViewDtos)) {
            throw new BusinessException("没有数据！");
        }
        if (personalViewDtos.size() > 5000) {
            throw new BusinessException("导出条数超过5000，请修改查询条件或联系管理员处理");
        }

        ExcelData data = new ExcelData();
        data.setName("人员工时统计列表");
        List<String> titles = Lists.newArrayList();
        titles.add("人员");
        titles.add("总工时（小时）");
        data.setTitles(titles);

        List<List<Object>> rows = Lists.newArrayList();
        for (PersonalViewDto personalViewDto : personalViewDtos) {
            List<Object> row = Lists.newArrayList();
            row.add(personalViewDto.getUserName());
            row.add(personalViewDto.getTimeWorked());
            rows.add(row);
        }
        data.setRows(rows);
        ExportExcelUtils.exportExcel(response, "personalTimeSummary.xlsx", data);
    }


    /**
     * 人员工时详情导出 合并
     *
     * @param filter
     * @param response
     * @throws IOException
     */
    @Override
    public void exportTimeDetailExcel(BasicInfoFilter filter, HttpServletResponse response) throws IOException {
        List<PersonalViewDto> personalViewDtos = selectTimeDetail(filter);

        if (CollectionUtils.isEmpty(personalViewDtos)) {
            throw new BusinessException("没有数据！");
        }
        if (personalViewDtos.size() > 5000) {
            throw new BusinessException("导出条数超过5000，请修改查询条件或联系管理员处理");
        }

        ExcelData data = new ExcelData();
        data.setName("人员工时详情列表（合并）");
        List<String> titles = Lists.newArrayList();
        titles.add("人员");
        titles.add("编号");
        titles.add("项目");
        titles.add("模块");
        titles.add("标题");
        titles.add("任务类型");
        titles.add("状态");
        titles.add("创建时间");
        titles.add("工时（小时）");
        data.setTitles(titles);

        List<List<Object>> rows = Lists.newArrayList();
        for (PersonalViewDto personalViewDto : personalViewDtos) {
            List<Object> row = Lists.newArrayList();
            row.add(personalViewDto.getUserName());
            row.add(personalViewDto.getJiraNum());
            row.add(personalViewDto.getPName());
            row.add(personalViewDto.getCName());
            row.add(personalViewDto.getTitle());
            row.add(personalViewDto.getIssueType());
            row.add(personalViewDto.getIssueStatus());
            row.add(personalViewDto.getIssueCreateTimeStr());
            row.add(personalViewDto.getTimeWorked());
            rows.add(row);
        }
        data.setRows(rows);
        ExportExcelUtils.exportExcel(response, "personalTimeDetail.xlsx", data);
    }

    /**
     * 人员工时详情导出 不合并
     *
     * @param filter
     * @param response
     * @throws IOException
     */
    @Override
    public void exportEveryTimeDetailExcel(BasicInfoFilter filter, HttpServletResponse response) throws IOException {
        List<PersonalViewDto> personalViewDtos = selectEveryTimeDetail(filter);

        if (CollectionUtils.isEmpty(personalViewDtos)) {
            throw new BusinessException("没有数据！");
        }
        if (personalViewDtos.size() > 5000) {
            throw new BusinessException("导出条数超过5000，请修改查询条件或联系管理员处理");
        }

        ExcelData data = new ExcelData();
        data.setName("人员工时详情列表（不合并）");
        List<String> titles = Lists.newArrayList();
        titles.add("人员");
        titles.add("编号");
        titles.add("项目");
        titles.add("模块");
        titles.add("标题");
        titles.add("任务类型");
        titles.add("状态");
        titles.add("创建时间");
        titles.add("工时（小时）");
        data.setTitles(titles);

        List<List<Object>> rows = Lists.newArrayList();
        for (PersonalViewDto personalViewDto : personalViewDtos) {
            List<Object> row = Lists.newArrayList();
            row.add(personalViewDto.getUserName());
            row.add(personalViewDto.getJiraNum());
            row.add(personalViewDto.getPName());
            row.add(personalViewDto.getCName());
            row.add(personalViewDto.getTitle());
            row.add(personalViewDto.getIssueType());
            row.add(personalViewDto.getIssueStatus());
            row.add(personalViewDto.getIssueCreateTimeStr());
            row.add(personalViewDto.getTimeWorked());
            rows.add(row);
        }
        data.setRows(rows);
        ExportExcelUtils.exportExcel(response, "personalTimeDetail.xlsx", data);
    }

}
